package com.example.demo.practice.enterprise;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.ExpandableListView;
import android.widget.ImageButton;
import android.widget.TextView;

import com.example.demo.practice.R;
import com.example.demo.practice.innerclass.NormalExpandableListAdapter;
import com.example.demo.practice.tool.ServerHttp;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

public class ScoringHistory extends AppCompatActivity {
    private ServerHttp serverHttp;
    private ExpandableListView expandableListView;
    private AutoCompleteTextView autoCompleteTextView;

    private String[] enterprises;
    private List<String> groupData;
    private List<List<String>> childData;

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            groupData = new ArrayList<String>();
            childData = new LinkedList<List<String>>();

            JSONArray jsonArray = serverHttp.getData();
            enterprises = new String[jsonArray.length()];
            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject json = null;
                try {
                    json = (JSONObject) jsonArray.get(i);
                    String adName = json.getString("admregion");
                    String name = json.getString("name");
                    if (groupData.contains(adName)) {
                        for (int j = 0; j < groupData.size(); j++) {
                            if (groupData.get(j).equals(adName)) {
                                childData.get(j).add(name);
                                break;
                            }
                        }
                    } else {
                        groupData.add(adName);
                        List<String> list = new ArrayList<String>();
                        list.add(name);
                        childData.add(list);
                    }
                    enterprises[i] = name;
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
            serverHttp.clear();
            NormalExpandableListAdapter normalExpandableListAdapter = new NormalExpandableListAdapter(groupData, childData);
            expandableListView.setAdapter(normalExpandableListAdapter);
            ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(ScoringHistory.this, android.R.layout.simple_list_item_1, enterprises);
            autoCompleteTextView.setAdapter(arrayAdapter);
            super.handleMessage(msg);
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTitle("评分历史");
        setContentView(R.layout.activity_scoring_history);
        expandableListView = (ExpandableListView) findViewById(R.id.listView);
        autoCompleteTextView = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView);
        serverHttp = new ServerHttp(handler);
        serverHttp.setUrl("enterprise");

        ImageButton searchButton = (ImageButton) findViewById(R.id.searchBtn);
        searchButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String name = autoCompleteTextView.getText().toString();
                boolean isExit = false;
                for (String nameE : enterprises) {
                    if (nameE.equals(name)) {
                        isExit = true;
                        break;
                    }
                }
                if (isExit) {
                    showResult(view, name);
                } else {
                    showNoResult(view, name);
                }
            }
        });
        //企业点击事件
        expandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
            @Override
            public boolean onChildClick(ExpandableListView expandableListView, View view, int i, int i1, long l) {
                TextView t=view.findViewById(R.id.label_expand_child);
                String name= (String) t.getText();
                showResult(view,name);
                return false;
            }
        });
    }

    /**
     * 查询到结果时
     * @param view
     * @param name
     */
    private void showResult(View view, final String name) {
        Snackbar.make(view, "已找到" + name, Snackbar.LENGTH_LONG)
                .setAction("前往", new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Intent intent=new Intent(ScoringHistory.this,ScoringHistoryShow.class);
                        intent.putExtra("name",name);
                        startActivity(intent);
                    }
                })
                .show();
    }

    /**
     * 无查询结果时
     * @param view
     * @param name
     */
    private void showNoResult(View view, String name) {
        Snackbar.make(view, "未找到" + name, Snackbar.LENGTH_LONG)
                .setAction("关闭", null)
                .show();
    }
}
